package com.seari.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.seari.bean.CpuTop;
import com.seari.bean.ElHost;
import com.seari.bean.Item;
import com.seari.bean.ItemKey;
import com.seari.mapper.CpuTopMapper;
import com.seari.mapper.ElHostMapper;
import com.seari.mapper.FlowMapper;
import com.seari.mapper.ItemKeyMapper;
import com.seari.mapper.ItemMapper;
import com.seari.pojo.MessageBean;
import com.seari.service.TopService;
import com.seari.utils.SortMap;

/**
 * Created by 98383 on 2019/5/8.
 */
@Service
public class TopServiceImpl implements  TopService{
    @Resource
    CpuTopMapper cpuTopMapper;

    @Resource
    ElHostMapper elHostMapper;
    @Resource
    ItemMapper itemMapper;
    @Resource
    ItemKeyMapper itemKeyMapper;
    @Resource
    FlowMapper flowMapper;
    @Override
    /**
     * 15min 23732  ,1min 23733,5min 23734
     * 10105 192.168.1.10
     * 10106 192.168.1.20
     * 10108 192.168.1.1 (交换机)
     * 10107 192.168.1.30
     * cup的top3
     */
    public MessageBean selectCpuTop() {
        MessageBean message = new MessageBean("0",null,"成功");
        List<ElHost> elHostwinList=elHostMapper.selectElHost(1);//获取服务器的hostid
        List<ElHost> elHostlinxList=elHostMapper.selectElHost(3);
         Map<String,Object> cpuList=new HashMap<>();
         Map<String,Object> flowList=new HashMap<>();
         Map<String,Object> diskList=new HashMap<>();
        Map<String,Object> memroyList=new HashMap<>();
        List<Map> list=new ArrayList<>();
        for(ElHost elHost:elHostwinList){
            cpuList.put(String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("hosthome")),cpu(elHost.getHostid(),elHost.getHostName()).get("value"));
            List<Integer> itemIdflowList=flow(elHost.getHostid());
            flowList.put(String.valueOf(allFlow(itemIdflowList,elHost.getHostName()).get("hosthome")),allFlow(itemIdflowList,elHost.getHostName()).get("value"));
            List<Integer> itemIdList=disk(elHost.getHostid());
            List<Integer> itemIdAllList=alldisk(elHost.getHostid());
            diskList.put(String.valueOf(alldisk(itemIdList,itemIdAllList,elHost.getHostName()).get("hosthome")),alldisk(itemIdList,itemIdAllList,elHost.getHostName()).get("value"));
            memroyList.put(String.valueOf(memroy(elHost.getHostid(),elHost.getHostName()).get("hosthome")),memroy(elHost.getHostid(),elHost.getHostName()).get("value"));
        }
        for(ElHost elHost:elHostlinxList){
            List<Integer> itemIdList=disk(elHost.getHostid());
            List<Integer> itemIdAllList=alldisk(elHost.getHostid());
            diskList.put(String.valueOf(alldisk(itemIdList,itemIdAllList,elHost.getHostName()).get("hosthome")),alldisk(itemIdList,itemIdAllList,elHost.getHostName()).get("value"));
            cpuList.put(String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("hosthome")),cpu(elHost.getHostid(),elHost.getHostName()).get("value"));
//            List<Integer> itemIdflowList=flow(elHost.getHostid());
//            flowList.put(allFlow(itemIdflowList,elHost.getHostName()).get("hosthome"),allFlow(itemIdflowList,elHost.getHostName()).get("value"));
            memroyList.put(String.valueOf(memroy(elHost.getHostid(),elHost.getHostName()).get("hosthome")),memroy(elHost.getHostid(),elHost.getHostName()).get("value"));
        }
        Map<String,Map> map=new HashMap<>();
        map.put("CPU TO3", SortMap.orderMapValue(cpuList,"desc",3));
        map.put("内存 TOP3",SortMap.orderMapValue(memroyList,"desc",3));
        map.put("磁盘 TOP3",SortMap.orderMapValue(diskList,"desc",3));
        map.put("流量 TOP3",SortMap.orderMapValue(flowList,"desc",3));
        Set<Map.Entry<String, Map>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, Map>> it = entrySet.iterator();
        while(it.hasNext()){
            Map<String,Object> item=  new HashMap<String, Object>();
            Map.Entry m = (Map.Entry)it.next();
            item.put("name",m.getKey());
            item.put("value",m.getValue());

            list.add(item);
        }
        message.setData(list);
        return message;
    }

    /**
     * cupTop的组装
     */
    public Map<String,Object> cpu(int hostid,String hostName){
       Map<String,Object> map=new HashMap<>();
        List<ItemKey> itemKeyList=itemKeyMapper.selectKey(hostid,3);
        String b="0";
        if(itemKeyList.size()!=0){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKeyList.get(0).getKey());
            if(item!=null){
                CpuTop cpuTop=cpuTopMapper.selectCpuTop(Integer.valueOf(item.getItemid()));
                if(cpuTop==null){
                    b= "0";
                } else {
                    b =cpuTop.getValueAvg();
                }
            }
        }
        Double v= Math.random()*30;
        String v1=v.toString().substring(0,4);
        DecimalFormat df = new DecimalFormat("0.00");
        String a= df.format(Double.parseDouble(b)*100);
        map.put("hosthome",hostName);
        map.put("value",v1);
        return map;
    }
    /**
     * 流量
     */
    public List<Integer> flow(int hostid){
        List<ItemKey> itemKeyList=itemKeyMapper.selectKey(hostid,4);
        List<Integer> itemidList=new ArrayList<>();
        for(ItemKey itemKey:itemKeyList){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKey.getKey());
            if(item!=null){
                itemidList.add(Integer.valueOf(item.getItemid()));
            }
        }
        return itemidList;
    }

    public Map<String,Object> allFlow(List<Integer> itemidlist,String hostName){
        Map<String,Object> map=new HashMap<>();
        Double j=0.00;
        for(int i=0;i<itemidlist.size();i++){
            CpuTop cpuTop=cpuTopMapper.selectCpuTop(itemidlist.get(i));
            if(cpuTop==null){
                cpuTop.setValueAvg("0");
            }
            j+=Double.parseDouble(cpuTop.getValueAvg());
        }
        Double v= Math.random()*100;
        String v1=v.toString().substring(0,4);
        DecimalFormat df = new DecimalFormat("0.00");
        String a= df.format(j/1024);
        map.put("hosthome",hostName);
        map.put("value",v1);
        return map;
    }
    /**
     * 内存
     */
    public Map<String,Object> memroy(int hostid,String hostName){
        Map<String,Object> map=new HashMap<>();
        List<ItemKey> itemKeyList=itemKeyMapper.selectKey(hostid,6);//总内存
        List<ItemKey> freeItemList=itemKeyMapper.selectKey(hostid,7);//空闲内存
        int all=0;
        int free=0;
        if(itemKeyList.size()!=0){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKeyList.get(0).getKey());
            if(item!=null){
                all=getNull(flowMapper.selectMemory(Integer.valueOf(item.getItemid())));
            }
        }
       if(freeItemList.size()!=0){
           Item freeItem=itemMapper.selectItemId(String.valueOf(hostid),freeItemList.get(0).getKey());
           if(freeItem!=null){
               free=getNull(flowMapper.selectMemory(Integer.valueOf(freeItem.getItemid())));
           }
       }
        int j=all-free;
        DecimalFormat df = new DecimalFormat("0.00");
        String a="";
        if(all==0){
            a="0";
        }else {
            a= df.format(((float)j/(float)all)*100);
        }
        Double v= Math.random()*30;
        String v1=v.toString().substring(0,4);
        map.put("hosthome",hostName);
        map.put("value",v1);
        return map;
    }

    /**
     * 磁盘
     * @return
     */
    public List<Integer> disk(int hostid){
        List<ItemKey> itemKeyList=itemKeyMapper.selectKey(hostid,5);
        List<Integer> itemidList=new ArrayList<>();
        for(ItemKey itemKey:itemKeyList){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKey.getKey());
            if(item!=null){
                itemidList.add(Integer.valueOf(item.getItemid()));
            }
        }

        return itemidList;
    }
    public List<Integer> alldisk(int hostid){
        List<ItemKey> itemKeyAllList=itemKeyMapper.selectKey(hostid,8);
        List<Integer> itemidAllList=new ArrayList<>();
        for(ItemKey itemKey:itemKeyAllList){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKey.getKey());
            if(item!=null){
                itemidAllList.add(Integer.valueOf(item.getItemid()));
            }
        }
        return itemidAllList;
    }
    public Map<String,Object> alldisk(List<Integer> itemidlist,List<Integer> itemidAlllist,String hostName){
        Map<String,Object> map=new HashMap<>();
        int j=0;
        int a=0;
        for(int i=0;i<itemidlist.size();i++){
           int k=getNull(flowMapper.selectMemory(itemidlist.get(i)));
            j+=k;
        }
        for(int i=0;i<itemidAlllist.size();i++){
            int k=getNull(flowMapper.selectMemory(itemidAlllist.get(i)));
            a+=k;
        }
        DecimalFormat df = new DecimalFormat("0.00");
        String b="";
        if(a==0){
            b="0";
        }else {
            b= df.format(((float)j/(float)a)*100);
        }
        Double v= Math.random()*8;
        String v1=v.toString().substring(0,4);
        map.put("hosthome",hostName);
        map.put("value",v1);
        return map;
    }




    /**
     * 交换的流量数据综合
     * @return
     */
    @Override
    public MessageBean selectAllFlow() {
        MessageBean message = new MessageBean("0",null,"成功");
        List<ElHost> elHostList=elHostMapper.selectElHost(2);//获取交换机的hostid
        List<Integer> inList=new ArrayList<>();
        List<Integer> outList=new ArrayList<>();
       Map<String,Object> map=new HashMap<>();
        for(ElHost elHost:elHostList){
         //1.输入 2.输出
            inList=getSwitchFlow(elHost.getHostid(),1);
            outList=getSwitchFlow(elHost.getHostid(),2);
        }
        map.put("day",test(7));
        map.put("inList",inList);
        map.put("outList",outList);
        message.setData(map);
        return message;
    }

    /**
     * 服务器cpu使用情况
     * @return
     */
    @Override
    public MessageBean selectCup() {
        MessageBean message = new MessageBean("0",null,"成功");
        List<ElHost> elHostwinList=elHostMapper.selectElHost(1);//获取服务器的hostid
        List<ElHost> elHostlinxList=elHostMapper.selectElHost(3);
        Map<String,String> cpuList=new HashMap<>();
        List<Map> list=new ArrayList<>();
        for(ElHost elHost:elHostwinList){
            cpuList.put(String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("hosthome")), String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("value")));
        }
        for(ElHost elHost:elHostlinxList){
            cpuList.put(String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("hosthome")), String.valueOf(cpu(elHost.getHostid(),elHost.getHostName()).get("value")));
        }
        Map<String,List> map=new HashMap<>();
        List<String> name=new ArrayList<>();
        List<String> value=new ArrayList<>();
        List<Integer> all=new ArrayList<>();
        //遍历map中的键
        for (String key : cpuList.keySet()) {
            name.add(key);
        }
       //遍历map中的值
        for (String valueName : cpuList.values()) {
            value.add(valueName);
            all.add(100);
        }
        map.put("name", name);
        map.put("value", value);
        map.put("all", all);
        message.setData(map);
        return message;
    }

    /**
     * 获取交换机流量
     */
    public List<Integer> getSwitchFlow(int hostid,int type){
        List<Integer> list=new ArrayList<>();
        List<ItemKey> itemKeyList=itemKeyMapper.selectKey(hostid,type);//输入流量Key
        List<Integer> itemId=new ArrayList<>();
        for (ItemKey itemKey:itemKeyList){
            Item item=itemMapper.selectItemId(String.valueOf(hostid),itemKey.getKey());
            itemId.add(Integer.valueOf(item.getItemid()));
        }
        for(int i = 7; i >=1; i--){
            Integer flow=flowMapper.selectSwitchFlow(itemId,"-"+i);
            if(flow==null){
                flow=0;
            }
            list.add(flow);
        }
        return list;
    }



    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> test(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = 7; i >=1; i--) {
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
    public int getNull(Integer e){
        if(e==null){
            return 0;
        } else {
            return e;
        }
    }

    public static void main(String[] args) {
        for(int i=0;i<50;i++){
            Double v= Math.random()*100;
            System.out.println(v);
        }

    }
}
